package com.yuelife.admin.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yuelife.admin.dao.CommentMapper;
import com.yuelife.admin.dao.DiscussionMapper;
import com.yuelife.admin.pojo.Comment;
import com.yuelife.admin.pojo.Discussion;
import com.yuelife.admin.pojo.MProduct;
import com.yuelife.admin.service.DiscussionService;
import com.yuelife.admin.util.DataGridView;
import com.yuelife.admin.vo.DiscussionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: yangsen
 * @Date: 2020/7/2 16:37
 */
@Service
public class DiscussionServiceImpl implements DiscussionService {
    @Autowired
    DiscussionMapper discussionMapper;
    @Autowired
    CommentMapper commentMapper;
    @Override
    public DataGridView selectAll(DiscussionVO discussion) {
        Page<Object> page= PageHelper.startPage(discussion.getPage(),discussion.getLimit());
        List<Discussion> list = discussionMapper.selectAll(discussion);
        return new DataGridView(page.getTotal(),list);
    }

    @Override
    public void deleteDiscussionById(Integer did) {
//        删除帖子
        discussionMapper.deleteByPrimaryKey(did);
//        先查到所有的回复
        List<Comment> list = commentMapper.selectByDiscussionid(did);
        for (Comment comment:list){
            if(comment.getComments().size()!=0){
//               如果下面还有回复，就通过id删掉所有的回复
               commentMapper.deletyByDiscussionId(comment.getId());
            }
//            删掉自己
            commentMapper.deleteByPrimaryKey(comment.getId());
        }
    }
}
